package com.lan;

import org.apache.commons.lang3.StringUtils;

public class CommonUtils {
    /**
     * 对mysql字符进行转义
     * @param x
     * @return
     */
    public static String escapeString(String x) {
        if(StringUtils.isBlank(x)) {
            return x;
        }
        if(!isNeedEscape(x)) {
            return x;
        }
        int stringLength = x.length();
        String parameterAsString = x;
        StringBuffer buf = new StringBuffer((int)((double)x.length() * 1.1000000000000001D));
        // 可以指定结果前后追加单引号：'
        //buf.append('\'');
        for(int i = 0; i < stringLength; i++)
        {
            char c = x.charAt(i);
            switch(c)
            {
                default:
                    break;

                case 0: // '\0'
                    buf.append('\\');
                    buf.append('0');
                    continue;

                case 10: // '\n'
                    buf.append('\\');
                    buf.append('n');
                    continue;

                case 13: // '\r'
                    buf.append('\\');
                    buf.append('r');
                    continue;

                case 92: // '\\'
                    buf.append('\\');
                    buf.append('\\');
                    continue;

                case 39: // '\''
                    buf.append('\\');
                    buf.append('\'');
                    continue;

                case 34: // '"'
                    buf.append('\\');
                    buf.append('"');
                    continue;

                case 26: // '\032'
                    buf.append('\\');
                    buf.append('Z');
                    continue;

            }
            buf.append(c);
        }
        // 可以指定结果前后追加单引号：'
        //buf.append('\'');
        parameterAsString = buf.toString();
        return parameterAsString;
    }
    /**
     * 判断内容是否需要进行转义
     * @param x
     * @return
     */
    public static boolean isNeedEscape(String x) {
        boolean needsHexEscape = false;
        if(StringUtils.isBlank(x)) {
            return needsHexEscape;
        }
        int stringLength = x.length();
        int i = 0;
        do
        {
            if(i >= stringLength)
                break;
            char c = x.charAt(i);
            switch(c)
            {
                case 0: // '\0'
                    needsHexEscape = true;
                    break;

                case 10: // '\n'
                    needsHexEscape = true;
                    break;

                case 13: // '\r'
                    needsHexEscape = true;
                    break;

                case 92: // '\\'
                    needsHexEscape = true;
                    break;

                case 39: // '\''
                    needsHexEscape = true;
                    break;

                case 34: // '"'
                    needsHexEscape = true;
                    break;

                case 26: // '\032'
                    needsHexEscape = true;
                    break;
            }
            if(needsHexEscape)
                break;
            i++;
        } while(true);
        return needsHexEscape;
    }
}
